package leetcode.code1358;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int numberOfSubstrings(String s) {
		char[] cs = s.toCharArray();
		int ans = 0;
		int l = 0, r = 0, flag = 0;
		int len = s.length();
		int[] cnts = new int[3];
		while (r < len) {
			while (flag != 7 && r < len) {
				int index = cs[r] - 'a';
				if (cnts[index] == 0)
					flag |= 1 << index;
				cnts[index]++;
				r++;
			}
			int L = 0;
			while (flag == 7) {
				int index = cs[l] - 'a';
				L++;
				if (--cnts[index] == 0) {
					flag ^= 1 << index;
				}
				l++;
			}
			ans += (len - r + 1) * L;
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(11, this.numberOfSubstrings("acbbcac"));
		H.compare(10, this.numberOfSubstrings("abcabc"));
		H.compare(3, this.numberOfSubstrings("aaacb"));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
